﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using NavKet.Models;
using System.IO;
namespace NavKet.Controllers
{
    public class ShopAdminController : Controller
    {
        //
        // GET: /ShopAdmin/ 
        MallEntities1 navKetEntity = new MallEntities1();

        public ActionResult Index(int param)
        {
            Session.Timeout = 30;

            Shop shop = navKetEntity.SelectShop(param).SingleOrDefault();
            //rest of Images
            List<ShopImage> shopImagesList = navKetEntity.SelectShopImagesByShopId(param).ToList();
            //delartments
            List<VW_DepartmentDetails> shopDepartmentList = navKetEntity.SelectVwDepartmentsDetailsByShopId(param).ToList();
            //phones
            List<ShopPhone> shopPhoneList = navKetEntity.SelectShopPhoneByShopId(param).ToList();
            //details
            List<ShopNew> shopnewsList = navKetEntity.SelectShopNewsByShopId(param).ToList();
            //services
            List<ShopService> ShopServiceList = navKetEntity.SelectShopServicesByShopId(param).ToList();

            ViewBag.shopImages = shopImagesList;
            ViewBag.shopDepartments = shopDepartmentList;
            ViewBag.shopPhones = shopPhoneList;
            ViewBag.shopnews = shopnewsList;
            ViewBag.ShopServices = ShopServiceList;


            return View(shop);

        }

        [HttpPost]
        public ActionResult Index(int param, FormCollection form)
        {

            var posted = Request.Files["MainImageUploader"];
            string imageName;
            #region Edit Shop
            Shop shop = navKetEntity.SelectShop(param).SingleOrDefault();

            string ShopName = form["txtShopName"].ToString();
            imageName = shop.mainImage;
            if (!string.IsNullOrEmpty(posted.FileName))
            {
                imageName = Path.GetFileName(posted.FileName);
                string path = "/Content/pics/" + imageName;
                posted.SaveAs(Server.MapPath(path));


            }
            #region EditShopTable
            navKetEntity.UpdateShop(shop.id, shop.shopNumber, ShopName, shop.rented, shop.FloorId, imageName);
            #endregion
            #endregion

            #region upload Shop imgs
            posted = Request.Files["uploader"];
            imageName = Path.GetFileName(posted.FileName);
            if (!string.IsNullOrEmpty(posted.FileName))
            {
                string path = "/Content/pics/" + imageName;
                posted.SaveAs(Server.MapPath(path));
                //insert
                navKetEntity.InsertShopImage(param, imageName);
            }
            #endregion

            return RedirectToAction("/Index/" + shop.id);
        }

        #region department
        public ActionResult EditDepartment(int param)
        {
            Session["DeptId"] = param;
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);
            if (mallId != 0) Session["MallId"] = mallId;
            else mallId = Convert.ToInt32(Session["MallId"]);
            ViewBag.MallCategories = navKetEntity.SelectMallCategoriesByMallId(mallId);
            ViewBag.DepartmentItems = navKetEntity.SelectItemsByDepartmentId(param);
            Department department = navKetEntity.SelectDepartment(param).SingleOrDefault();
            return View(department);
        }
        [HttpPost]
        public ActionResult EditDepartment(int param, FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            string DepartmentName = form["txtDeptName"].ToString();
            string Descreption = form["txtDeptDecreption"].ToString();
            int catid = int.Parse(form["catid"].ToString());
            #region update
            // Department department = navKetEntity.SelectDepartmentByNameAndShopId(null, shopid).SingleOrDefault();

            navKetEntity.UpdateDepartment(param, DepartmentName, catid, Descreption);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        public ActionResult AddDepartment()
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            int mallId = Convert.ToInt32(Request.QueryString["mallId"]);

            List<Category> mallCategoryList = navKetEntity.SelectMallCategoriesByMallId(mallId).ToList();//navKetEntity.SelectMallCategoriesByMallId(mallId).ToList();
            ViewBag.MallCategories = mallCategoryList;

            return View(shopId);

        }
        [HttpPost]
        public ActionResult AddDepartment(FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            string DepartmentName = form["txtDeptName"].ToString();
            string Descreption = form["txtDeptDecreption"].ToString();
            int catid = int.Parse(form["catid"].ToString());
            #region insert
            navKetEntity.InsertDepartment(DepartmentName, Convert.ToInt32(Session["shopId"]), catid, Descreption);

            //navKetEntity.UpdateDepartment(param, DepartmentName, catid, Descreption);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        public ActionResult DeleteDepartment(int param)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            #region delete
            navKetEntity.DeleteDepartment(param);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        #endregion
        #region shopService
        public ActionResult DeleteShopService(int param)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            #region delete
            navKetEntity.DeleteShopService(param);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        public ActionResult EditShopService(int param)
        {
            //services
            ShopService ShopService = navKetEntity.SelectShopService(param).SingleOrDefault();
            return View(ShopService);
        }
        [HttpPost]
        public ActionResult EditShopService(int param, FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            string name = form["txtServiceName"];
            string Desreption = form["txtServiceDescreption"];
            string RequireData = form["ReuireData"];
            navKetEntity.UpdateShopService(param, name, Desreption, RequireData, shopId);

            return RedirectToAction("/index/" + shopId);
        }

        public ActionResult AddShopService()
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            return View(shopId);
        }
        [HttpPost]
        public ActionResult AddShopService(FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            string name = form["txtServiceName"];
            string Desreption = form["txtServiceDescreption"];
            string RequireData = form["ReuireData"];
            navKetEntity.InsertShopService(name, Desreption, RequireData, shopId);

            return RedirectToAction("/index/" + shopId);
        }
        #endregion
        #region shopNews
        public ActionResult DeleteShopNews(int param)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            #region delete
            //delete
            navKetEntity.DeleteShopNews(param);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        public ActionResult EditShopNews(int param)
        {
            ShopNew shopnews = navKetEntity.SelectShopNews(param).SingleOrDefault();
            return View(shopnews);
        }
        [HttpPost]
        public ActionResult EditShopNews(int param, FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            #region edit shop news
            string Title = form["txtTitle"].ToString();
            string Content = form["txtNewsContent"].ToString();
            navKetEntity.updateShopnews(param, Title, Content);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        public ActionResult AddShopNews()
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            return View(shopId);
        }
        [HttpPost]
        public ActionResult AddShopNews(FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            #region Add shop news
            string Title = form["txtTitle"].ToString();
            string Content = form["txtNewsContent"].ToString();
            navKetEntity.InsertShopNews(Title, Content, DateTime.Now, Convert.ToInt32(Session["shopId"]));
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        #endregion
        #region item information
        public ActionResult DeleteItemInformation(int param)
        {
            long ItemId = Convert.ToInt64(Request.QueryString["itemId"]);
            #region delete
            navKetEntity.DeleteItemInformation(param);
            #endregion
            return RedirectToAction("EditItem/" + ItemId);
        }
        public ActionResult EditItemInformation(int param)
        {
            ItemInformation ItemInformation = navKetEntity.SelectItemInformation(param).SingleOrDefault();
            return View(ItemInformation);
        }
        [HttpPost]
        public ActionResult EditItemInformation(int param, FormCollection form)
        {
            long ItemId = Convert.ToInt64(Request.QueryString["itemId"]);
            #region edititem information
            string FeatureName = form["txtFeatureName"].ToString();
            string FeatureDescretion = form["txtFeatureDescretion"].ToString();
            navKetEntity.UpdateItemInformation(param, FeatureName, FeatureDescretion);
            #endregion
            return RedirectToAction("EditItem/" + ItemId);
        }
        public ActionResult AddItemInformation()
        {
            long itemId = Convert.ToInt64(Request.QueryString["itemId"]);
            return View(itemId);
        }
        [HttpPost]
        public ActionResult AddItemInformation(FormCollection form)
        {
            long ItemId = Convert.ToInt64(Request.QueryString["itemId"]);
            #region Additem information
            long itemId = Convert.ToInt64(Request.QueryString["itemId"]);
            string FeatureName = form["txtFeatureName"].ToString();
            string FeatureDescretion = form["txtFeatureDescretion"].ToString();
            navKetEntity.InsertItemInformation(FeatureName, FeatureDescretion, itemId);
            #endregion
            return RedirectToAction("EditItem/" + ItemId);
        }
        #endregion
        #region ShopImgs
        public ActionResult DeleteShopImage(string param)
        {

            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            #region delete
            //delete
            navKetEntity.DeleteShopImage(shopId, param);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }

        #endregion
        #region ItemImgs
        public ActionResult DeleteItemImage(string param)
        {

            long itemid = Convert.ToInt64(Request.QueryString["itemId"]);
            #region delete
            //delete
            navKetEntity.DeleteItemImage(itemid, param);
            #endregion
            return RedirectToAction("EditItem/" + itemid);
        }

        #endregion
        #region shop Phone
        public ActionResult DeletShopPhone(int param)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            #region delete
            navKetEntity.DeleteShopPhone(param);
            #endregion
            return RedirectToAction("/index/" + shopId);
        }
        public ActionResult EditShopPhone(int param)
        {
            //services
            ShopPhone ShopPhone = navKetEntity.SelectShopPhone(param).SingleOrDefault();
            return View(ShopPhone);
        }
        [HttpPost]
        public ActionResult EditShopPhone(int param, FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            string number = form["txtPhoneNumber"];
            string Type = form["PhoneType"];
            navKetEntity.InsertShopPhone(shopId, number, Type);
            return RedirectToAction("/index/" + shopId);
        }

        public ActionResult AddShopPhone()
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            return View(shopId);
        }
        [HttpPost]
        public ActionResult AddShopPhone(FormCollection form)
        {
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            string number = form["txtPhoneNumber"];
            string Type = form["PhoneType"];
            navKetEntity.InsertShopPhone(shopId, number, Type);
            return RedirectToAction("/index/" + shopId);
        }
        #endregion
        #region item
        //DeleteItem
        public ActionResult DeleteItem(int param)
        {
            int DepartmentId = Convert.ToInt32(Request.QueryString["deptId"]);
            #region delete
            navKetEntity.DeleteItem(param);
            #endregion
            return RedirectToAction("/EditDepartment/" + DepartmentId);
        }
        public ActionResult EditItem(long param)
        {
            int shopid = Convert.ToInt32(Request.QueryString["shopId"]);
            if (shopid != 0) Session["shopId"] = shopid;
            else shopid = Convert.ToInt32(Session["shopId"]);
            Item item = navKetEntity.SelectItem(param).SingleOrDefault();

            List<Department> dd = navKetEntity.SelectDepartmentByShopId(shopid).ToList();
            ViewBag.ShopDepartments = dd;
            ViewBag.ItemImages = navKetEntity.SelectItemImagesByItemID(param);
            ViewBag.ItemInformation = navKetEntity.SelectItemInformationByItemId(param);
            return View(item);
        }
        [HttpPost]
        public ActionResult EditItem(long param, FormCollection form)
        {
            int deptId = Convert.ToInt32(Request.QueryString["deptId"]);
            #region Item Main Img
            var posted = Request.Files["MainImageUploader"];
            string imageName;
            imageName = navKetEntity.SelectItemMainImageByItemId(param).SingleOrDefault();
            if (!string.IsNullOrEmpty(posted.FileName))
            {
                imageName = Path.GetFileName(posted.FileName);
                string path = "/Content/pics/" + imageName;
                posted.SaveAs(Server.MapPath(path));

            }
            #endregion

            #region edit shop news
            string name = form["txtItemName"].ToString();
            decimal price = Convert.ToDecimal(form["txtPrice"]);
            int Quantity = Convert.ToInt32(form["txtQuantity"]);
            int DepartmentId = Convert.ToInt32(form["Department"]);
            string model = form["txtModel"].ToString();
            string releasedYear = form["txtYear"].ToString();
            navKetEntity.UpdateItem(param, name, price, Quantity, DepartmentId, imageName, model, releasedYear);
            #endregion

            #region upload Shop imgs
            posted = Request.Files["uploader"];
            imageName = Path.GetFileName(posted.FileName);
            if (!string.IsNullOrEmpty(posted.FileName))
            {
                string path = "/Content/pics/" + imageName;
                posted.SaveAs(Server.MapPath(path));
                //insert
                navKetEntity.InsertItemImage(param, imageName);
            }
            #endregion

            return RedirectToAction("/EditDepartment/" + deptId);
        }
        public ActionResult AddItem()
        {
            int DepartmentId = Convert.ToInt32(Request.QueryString["deptId"]);
            int shopId = Convert.ToInt32(Request.QueryString["shopId"]);
            ViewBag.ShopDepartments = navKetEntity.SelectDepartmentByShopId(shopId).ToList();

            return View(DepartmentId);
        }
        [HttpPost]
        public ActionResult AddItem(FormCollection form)
        {
            int deptId = Convert.ToInt32(Request.QueryString["deptId"]);
            #region Item Main Img
            var posted = Request.Files["MainImageUploader"];
            string imageName = null;
            if (!string.IsNullOrEmpty(posted.FileName))
            {
                imageName = Path.GetFileName(posted.FileName);
                string path = "/Content/pics/" + imageName;
                posted.SaveAs(Server.MapPath(path));

            }
            #endregion

            #region edit shop news
            string name = form["txtItemName"].ToString();
            decimal price = Convert.ToDecimal(form["txtPrice"]);
            int Quantity = Convert.ToInt32(form["txtQuantity"]);
            int DepartmentId = Convert.ToInt32(form["Department"]);
            string model = form["txtModel"].ToString();
            string releasedYear = form["txtYear"].ToString();
            navKetEntity.InsertItem(name, price, Quantity, DepartmentId, imageName, model, releasedYear);
            #endregion
            return RedirectToAction("/EditDepartment/" + deptId);
        }
        #endregion
        void testc()
        {
            int shopid = Convert.ToInt32(Session["shopId"]);
            Shop shop = navKetEntity.SelectShop(shopid).SingleOrDefault();

            #region itemOffer
            #region insert
            int? offerId = navKetEntity.InsertItemOfferReturnId(null, null, null).SingleOrDefault();
            navKetEntity.InsertItem_ItemOffer(null, offerId);
            #endregion
            #region update
            navKetEntity.UpdateItemOffer(null, null, null, null, null);
            #endregion
            #region delete ItemOffer
            navKetEntity.DeleteItemOffer(null);
            #endregion
            #region delete item from offer
            navKetEntity.DeleteItem_ItemOffer(null, null);
            #endregion
            #endregion
        }

    }
}